import request from '../request.js'

/**
 * 根据isbn查询易源接口
 * @param isbn
 * @return {promise<unknown>}
 * */
export const getBookInfo = (isbn) => new Promise(async (resolve, reject) => {
    console.log('getBookInfo')
    try {
		if(!isbn){
			reject('isbn不能为空！')
		}
        await request.get(`/book/fromApi/${isbn}`).then(res=>{
            if (res.data.code == 200){
                resolve(res.data.data)
            }else {
                reject(res.data.msg)
            }
        }).catch(reason => {
            reject(reason)
        })
    }catch (err){
		reject(err)
    }
})

/**
 * 录入书本数据
 */
export const collectBookInfo = (params)=> new Promise(async (resolve,reject) => {
	console.log('collectBookInfo');
	try {
	    await request.post('/book',params).then(res=>{
	        if (res.data.code == 200){
	            resolve(res.data.data)
	        }else {
	            reject(res.data.msg)
	        }
	    }).catch(reason => {
	        reject(reason)
	    })
	}catch (err){
	    reject(err)
	}
})

/**
 * 获取分类列表
 * @returns {Promise<unknown>}
 */
export const getSubjectList = () => new Promise(async (resolve, reject) => {
    console.log('getSubjectList')
    try {
        await request.get('/subject').then(res=>{
            if (res.data.code == 200){
                resolve(res.data.data)
            }else {
                reject(res.data.msg)
            }
        }).catch(reason => {
            reject(reason)
        })
    }catch (err){
        reject(err)
    }
})